﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace RealState.Utils
{
    public class Imaging
    {
        public static bool IsImageExtension(string filename)
        {
            string str = Path.GetExtension(filename).ToLower();
            if ((!(str == ".jpg") && !(str == ".jpeg")) && (!(str == ".png") && !(str == ".gif")))
            {
                return false;
            }
            return true;
        }

        public static string GetFileExtension(string sFileName)
        {
            sFileName = sFileName.Trim();

            if (String.IsNullOrEmpty(sFileName))
            {
                return String.Empty;
            }

            string sExtension = String.Empty;

            char[] cArr = sFileName.ToCharArray();

            int iIndex = 0;

            for (int i = cArr.Length - 1; i > -1; i--)
            {
                if (cArr[i].Equals('.'))
                {
                    iIndex = i;
                    break;
                }
            }

            if (iIndex > 0)
            {
                for (int i = iIndex + 1; i < cArr.Length; i++)
                {
                    sExtension += cArr[i];
                }
            }

            return sExtension.ToLower();

        }

        public static byte[] ResizeImageWithAspect(int Width, byte[] picture, string origFilename, string mappedTempFolder, bool HeightIsSame)
        {
            string extension = Path.GetExtension(origFilename);
            string str2 = Guid.NewGuid().ToString();
            string path = mappedTempFolder.SlashRight() + str2 + extension;
            FileStream stream = new FileStream(path, FileMode.Create);
            stream.Write(picture, 0, picture.Length);
            stream.Flush();
            stream.Close();
            stream.Dispose();
            Image image = Image.FromFile(path);
            int width = Width;
            float num = ((float)Width) / ((float)image.Width);
            int height = Convert.ToInt32((float)(image.Height * num));
            int num4 = height;
            if (HeightIsSame)
            {
                num4 = Width;
            }
            Image image2 = new Bitmap(width, num4);
            Graphics graphics = Graphics.FromImage(image2);
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle rect = new Rectangle(0, 0, width, height);
            if (!HeightIsSame)
            {
                graphics.FillRectangle(Brushes.White, rect);
                graphics.DrawImage(image, rect);
            }
            else
            {
                graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, width, num4));
                graphics.DrawImage(image, rect);
            }
            string filename = mappedTempFolder.SlashRight() + Guid.NewGuid().ToString() + ".jpg";
            image2.Save(filename, ImageFormat.Jpeg);
            graphics.Flush();
            graphics.Dispose();
            image.Dispose();
            image2.Dispose();
            FileStream stream2 = new FileStream(filename, FileMode.Open);
            byte[] buffer = new byte[stream2.Length];
            stream2.Read(buffer, 0, buffer.Length);
            stream2.Flush();
            stream2.Close();
            stream2.Dispose();
            try
            {
                File.Delete(path);
                File.Delete(filename);
            }
            catch
            {
            }
            return buffer;
        }
    }
}
